package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.Board;
import edu.school.service.BoardsService;
import edu.school.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

@Controller
public class BoardController {
    @Autowired
    BoardsService service;
    @RequestMapping(value = "/board/toAdd")
    public String toAdd() {
        return "board/add";
    }
    @RequestMapping(value = "/board/add",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(Board record){
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreate_time(timeStamp);
        if(service.save(record)){
            return "<script>alert('发布成功');location.href='/board/list';</script>";
            /*  return "<script>alert('注册成功');location.href='/toLogin.action';</script>";*/
        }else{
            return "<script>alert('发布失败');history.go(-1);</script>";
        }

    }
    @RequestMapping(value = "/board/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(Board record,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
        PageInfo<Board> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        return "/board/list";
    }
    @RequestMapping(value = "/board/query")
    public String query( Integer id, Model model){
        Board record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);
        }
        return "board/edit";
    }
    @RequestMapping(value = "/board/detail")
    public String detail(Integer id, Model model) {
        Board record = service.findById(id);
        if (null != record) {
            model.addAttribute("record", record);
        }
        return "board/detail";
    }
    //前台详情页
    @RequestMapping(value = "/board/detail2")
    public String qtdetail(Integer id, Model model) {
        Board record = service.findById(id);
        if (null != record) {
            model.addAttribute("record", record);
        }
        return "client/board_detail";
    }
    //公告修改功能
    @RequestMapping(value = "/board/edit",method = {RequestMethod.GET,RequestMethod.POST},produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String edit(Board record,HttpSession session){
        if(service.update(record)){
            return "<script>alert('修改成功');location.href='/board/list';</script>";

        }else{
            return "<script>alert('修改失败');history.go(- 1);</script>";
        }

    }


    @RequestMapping("/board/delete")
    @ResponseBody
    public String ajaxdelete(Integer id,String filename) {
        boolean b =service.deleteBoard(id);
        System.out.println(filename+"文件名称");
        WebUtils.deleteImg(filename);
        if(b){
            return "ok";
        }
        return "fail";

    }
    //批量删除
    @RequestMapping("/board/deletemany")
    @ResponseBody
    public String  deletemany(String ids) {
        System.err.println(ids);
        String[] arrayids=ids.split(",");//把数组里的值逗号隔开
        System.out.println("批量删除成功");

        service.deleteMany(arrayids);
        return "ok";//返回给ajax



    }
}
